<?php


namespace App\Models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Model as BaseModel;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;

abstract class Model extends BaseModel
{

    //use SoftDeletes;

    private static $hiddenArr = [];
    private static $appendsArr = [];


    /** 主键id为uuid  start **/
//    public $incrementing = false;
//    protected $keyType = 'string'; //
//    /**
//     * 主键自动生成uuid
//     */
//    public static function boot()
//    {
//        parent::boot();
//        static::creating(function ($model) {
//            $model->{$model->getKeyName()} = (string)Str::uuid();
//        });
//    }
    /** 主键id为uuid  end **/


    /**
     * 动态设置 hidden
     * @param array $hidden
     * @return static
     */
    public static function setHiddenArr(array $hidden) {
        self::$hiddenArr = $hidden;
        return (new static);
    }

    /**
     * 动态设置 hidden
     * @param array $appends
     * @return static
     */
    public static function setAppendsArr(array $appends) {
        self::$appendsArr = $appends;
        return (new static);
    }

    /**
     * @overwrite Create a new instance of the given model.
     *
     * @param array $attributes
     * @param false $exists
     * @return Model
     */
    public function newInstance($attributes = [], $exists = false)
    {
        $model = parent::newInstance($attributes, $exists);

        $model->setHidden(array_unique(array_merge($this->hidden, self::$hiddenArr)));
        $model->setAppends(array_unique(array_merge($this->appends, self::$appendsArr)));

        return $model;
    }

    /**
     * 格式化key   user_id ~~ userId
     * @return array
     */
    public function toArray()
    {
        foreach (parent::toArray() as $k => $v){
            $data[lcfirst(Str::studly($k))] = $v;
        }
        return $data ?? [];
    }

    /**
     * Prepare a date for array / JSON serialization. 修复时间格式化不合适问题
     *
     * @param  DateTimeInterface  $date
     * @return string
     */
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }
}
